# -*- coding: utf-8 -*-

'''eof
name:未结清信贷交易最严重五级分类
code:EC92_Unsettled5ClassWorstCheck
tableName:
columnName:
groups:行内二代企业征信数据
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def isfloat(x):
    '''
    判断x为float类型且大于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) > 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def EC92_Unsettled5ClassWorstCheck():
    data = EC_DSJ_2D.get('data')
    # 信用提示信息,出现次数[1..1],下级字段出现次数[1..1]
    EBA = getTable(data, 'EBA', 1, 1)
    if EBA == [{}]:
        return u'缺失值'
    elif EBA == 0:
        return 0
    else:
        #信用提示信息单元,出现次数[1..1]，下级字段出现次数[0..1]
        EB01 = getTable(EBA[0], 'EB01', 1, 0)
        if EB01 == [{}]:
            return u"缺失值"
        elif EB01 == 0:
            return 0
        else:
            #信贷交易提示信息段，出现次数[0..1]，下级字段出现次数[1..1]
            EB01A = getTable(EB01[0], 'EB01A', 0, 1)
            if EB01A == [{}]:
                return u"缺失值"
            elif EB01A == 0:
                return 0
            else:
                #不良类借贷交易余额
                EB01AJ04 = EB01A[0].get('EB01AJ04')
                #不良类担保交易余额
                EB01AJ07 = EB01A[0].get('EB01AJ07')
                if isfloat(EB01AJ04) or isfloat(EB01AJ07):
                    return u"不良"
                else:
                    #关注类借贷交易余额
                    EB01AJ03 = EB01A[0].get('EB01AJ03')
                    #关注类担保交易余额
                    EB01AJ06 = EB01A[0].get('EB01AJ06')
                    if isfloat(EB01AJ03) or isfloat(EB01AJ06):
                        return u"关注"
                    else:
                        #借贷交易余额
                        EB01AJ01 = EB01A[0].get('EB01AJ01')
                        #担保交易余额
                        EB01AJ05 = EB01A[0].get('EB01AJ05')
                        if isfloat(EB01AJ01) or isfloat(EB01AJ05):
                            return u"正常"
                        else:
                            return u"缺失值"
                
result = EC92_Unsettled5ClassWorstCheck()
